<template>
<div style="width: 100%">
  <a-card class="card" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
        <a-col :md="6" :sm="12">
          <a-form-item label="准考证号">
            <a-input placeholder="请输入..." v-model="queryParam.ksbh"/>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="12">
          <a-form-item label="录取院系">
          <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.lqyxsdm" @change="yxChange">
            <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
          </a-select>
        </a-form-item>
        </a-col>
        <a-col :md="6" :sm="12" >
          <a-form-item label="录取专业">
          <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.lqzym">
            <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
          </a-select>
        </a-form-item>
        </a-col>
        <a-col :md="6" :sm="12">
            <a-form-item label="复试成绩所占比例">
                <a-input-number style="width:90%" placeholder="请输入..." v-model="postParams.fscjqz" /> %
            </a-form-item>
          </a-col>
       </a-row>
         
        <div style="text-align:center;margin: 10px 0">
          <a-button type="primary" @click="loadData">查询</a-button>
          <a-button type="primary" @click="handleExport">导出</a-button>
          <a-button type="primary" @click="handleEdit" :disabled="EditStop">录入(修改)</a-button>
          <a-button type="primary" @click="handleSave" :disabled="!EditStop">保存</a-button>
          <a-button type="primary" @click="zhCompute">综合评定计算</a-button>
        </div>
      </a-form>
    </div>
    </a-card>
    <a-alert class="card"  message="※综合评定成绩 = （业务课一 + 业务课二）/2 * 初试成绩权重 + 复试成绩 * 复试成绩权重" banner />
    <a-card :bordered="false">
    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :dataSource="data"
      :pagination="pagination"
			@change="handleTableChange"
    >
      <template slot="fscj" slot-scope="text,record">
         <a-input-number placeholder="请输入..." style="max-width:100px;min-width:50px" :key="record.id" :defaultValue="text" :disabled="!EditStop" @change="e => handleChange(record.id,'fscj',e)"/>
      </template>
      <template slot="zhpd" slot-scope="text,record">
         <a-input placeholder="请输入..." style="max-width:100px;min-width:50px" :key="record.id" :defaultValue="text" :disabled="!EditStop" @change="e => zhChange(record.id,'zhpd',e.target.value)"/>
      </template>

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
    </a-card>
</div>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = []

export default {
  components: {
    STable
  },
  data () {
    return {
      loading: false,
      data: [],
      EditStop: false,
      // 查询参数
      queryParam: {

      },
      pagination: {
        current:1
      },
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '准考证号',
          dataIndex: 'ksbh'
        },
        {
          title: '姓名',
          dataIndex: 'ksxm'
        },
        {
          title: '录取院系',
          dataIndex: 'dwmc'
        },
        {
          title: '录取专业',
          dataIndex: 'zymc'
        },
        {
          title: '研究方向',
          dataIndex: 'yjfxmc'
        },
        {
          title: '业务课一',
          dataIndex: 'ywkycj'
        },
        {
          title: '业务课二',
          dataIndex: 'ywkecj'
        },
        {
          title: '复试成绩',
          dataIndex: 'fscj',
          scopedSlots: { customRender: 'fscj' }
        },
        {
          title: '复试成绩权重(%)',
          dataIndex: 'fscjqz'
        },
        {
          title: '综合评定',
          dataIndex: 'zhpd',
          scopedSlots: { customRender: 'zhpd' }
        }
      ],
      selectMenu: {
        yx: [] // 院系
      },
      recruitEditData: {}
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()

    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.lqyxsdm=res[0].data[0].dwh
        this.$api.base.majorAll({lsdwh: this.queryParam.lqyxsdm}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
       })
        this.loadData({
        pageSize:10,
        pageNum:1
        })
      })
    },
    // 选择院系
    yxChange (code) {
      this.$set(this.queryParam, 'lqzym', '')
      this.$set(this.selectMenu,'zy','')
      if(code){
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
      }
    },
     // 加载数据
    loadData(parameter = {}) {
      if(!parameter.pageNum){
         this.pagination.current = 1
         parameter.pageNum = 1
       }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.doctorRecruit.secondTryList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
          this.data = res.data.rows;
        }
        this.loading = false;
      })
    },
		// 分页
		handleTableChange (pagination, filters, sorter) {
      console.log(pagination);
      const pager = { ...this.pagination };
      pager.current = pagination.current;
      this.pagination = pager;
      this.loadData({
        pageSize: 10,
        pageNum: pagination.current
      });
    },
    // 选择招生年度
    zsndChange () {

    },
    // 导出
    handleExport (data) {
      console.log('导出')
      this.$export('admissions/bsksxx/bscjlrexport',this.queryParam)
    },
    // 修改
    handleEdit (data) {
      if (this.EditStop) {
        return
      }
      this.EditStop = true
      console.log('修改')
    },
    // 保存
    handleSave () {
      this.EditStop = false;
      let params=[],p={}
      this.data.map(item=>{
        p={}
        p=Object.assign(p, {id:item.id})
        p=Object.assign(p, {fscj:item.fscj})
        p=Object.assign(p, {zhpd:item.zhpd})
        params.push(p)
      })
      this.$api.doctorRecruit.secondTryAdd(params).then(res=>{
        this.loadData({
          pageSize:10,
          pageNum:this.pagination.current
        })
      })
    },
    //修改综合成绩
    zhChange(key, dataIndex, value) {
      // console.log(dataIndex)
      const dataSource = [...this.data]
      const target = dataSource.find(item => item.id === key)
      if (target) {
        target[dataIndex] = value
        this.data = dataSource
      }
       console.log(this.data)
    },
    // 修改成绩
    handleChange (key, dataIndex, value) {
      console.log(key)
      console.log(dataIndex)
      console.log(value)
      const dataSource = [...this.data]
      const target = dataSource.find(item => item.id === key)
      if (target) {
        target[dataIndex] = value
        this.data = dataSource
      }
      // console.log(this.data)
    },
    //综合成绩计算
    zhCompute(){
      let params={
        fscjqz:this.postParams.fscjqz,
        lqyxsdm:this.queryParam.lqyxsdm,
        lqzym:this.queryParam.lqzym
      }
       this.loading = true;
      this.$api.doctorRecruit.secondTryCom(params).then(res=>{
        this.$message.success(res.msg)
        this.data=[]
        this.loadData({
          pageSize:10,
          pageNum:this.pagination.current
        })

      }).finally(()=>{
        this.loading = false;
      })
    }
  }
}
</script>

<style lang="less" scoped>
.card{
  margin-bottom: 20px;
}
</style>
